package com.scmt.hmzd.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scmt.hmzd.entity.HmCouponUser;
import com.scmt.hmzd.service.IHmCouponService;

import javax.servlet.http.HttpServletResponse;

import com.scmt.hmzd.service.IHmCouponUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.scmt.core.common.utils.ResultUtil;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.Result;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.core.common.utils.SecurityUtil;
import com.scmt.hmzd.entity.HmCoupon;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scmt.core.common.enums.LogType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author
 **/
@RestController
@Api(tags = " 优惠券数据接口")
@RequestMapping("/scmt/hmCoupon")
public class HmCouponController {
    @Autowired
    private IHmCouponService hmCouponService;
    @Autowired
    private IHmCouponUserService iHmCouponUserService;
    @Autowired
    private SecurityUtil securityUtil;

    /**
     * 功能描述：新增优惠券数据
     *
     * @param hmCoupon 实体
     * @return 返回新增结果
     */
    @ApiOperation("新增优惠券数据")
    @PostMapping("addHmCoupon")
    public Result<Object> addHmCoupon(@RequestBody HmCoupon hmCoupon) {
        try {
            hmCoupon.setCreateTime(new Date());
            boolean res = hmCouponService.save(hmCoupon);
            if (res) {
                return ResultUtil.data(res, "保存成功");
            } else {
                return ResultUtil.data(res, "保存失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("保存异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：更新数据
     *
     * @param hmCoupon 实体
     * @return 返回更新结果
     */
    @ApiOperation("更新优惠券数据")
    @PostMapping("updateHmCoupon")
    public Result<Object> updateHmCoupon(@RequestBody HmCoupon hmCoupon) {
        if (hmCoupon.getCouponId() != null) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            boolean res = hmCouponService.updateById(hmCoupon);
            if (res) {
                return ResultUtil.data(res, "修改成功");
            } else {
                return ResultUtil.data(res, "修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("保存异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：根据主键来删除数据
     *
     * @param ids 主键集合
     * @return 返回删除结果
     */
    @ApiOperation("根据主键来删除优惠券数据")
    @PostMapping("deleteHmCoupon")
    public Result<Object> deleteHmCoupon(@RequestParam String[] ids) {
        if (ids == null || ids.length == 0) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            List<String> idList = Arrays.asList(ids);
            boolean res = hmCouponService.removeByIds(idList);
            if (res) {
                //后端删除了优惠券以后，需要把用户领取的优惠券也进行删除了

                QueryWrapper<HmCouponUser> hmCouponUserQueryWrapper = new QueryWrapper<>();
                hmCouponUserQueryWrapper.lambda().in(HmCouponUser::getCouponId, idList);
                iHmCouponUserService.remove(hmCouponUserQueryWrapper);
                return ResultUtil.data(res, "删除成功");
            } else {
                return ResultUtil.data(res, "删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("删除异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：根据主键来获取数据
     *
     * @param id 主键
     * @return 返回获取结果
     */
    @ApiOperation("根据主键来获取优惠券数据")
    @GetMapping("getHmCoupon")
    public Result<Object> getHmCoupon(@RequestParam(name = "id") String id) {
        if (StringUtils.isBlank(id)) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            HmCoupon res = hmCouponService.getById(id);
            if (res != null) {
                return ResultUtil.data(res, "查询成功");
            } else {
                return ResultUtil.data(res, "查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：实现分页查询
     *
     * @param searchVo 需要模糊查询的信息
     * @param pageVo   分页参数
     * @return 返回获取结果
     */
    @ApiOperation("分页查询优惠券数据")
    @GetMapping("queryHmCouponList")
    public Result<Object> queryHmCouponList(HmCoupon hmCoupon, SearchVo searchVo, PageVo pageVo) {
        try {
            IPage<HmCoupon> result = hmCouponService.queryHmCouponListByPage(hmCoupon, searchVo, pageVo);
            return ResultUtil.data(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询异常:" + e.getMessage());
        }
    }

}
